package com.gitee.huxing.course.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.gitee.huxing.course.order.domain.module.PlatFormOrderDO;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


/**
 * @Description: 单元功能测试
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.course.order.controller
 * @ClassName: JunitTestController
 * @Author: huxingmale
 * @DateTime: 2021-09-25 16:33
 */
@Slf4j
// 获取启动类，加载配置，确定装载 Spring 程序的装载方法，它回去寻找 主配置启动类（被 @SpringBootApplication 注解的）
@SpringBootTest
// 让 JUnit 运行 Spring 的测试环境， 获得 Spring 环境的上下文的支持
@RunWith(SpringRunner.class)
public class JunitTestBatchController {

    private MockMvc mvc;

    @Autowired
    private WebApplicationContext ctx;

    @Before
    public void before(){
        mvc = MockMvcBuilders.webAppContextSetup(ctx).build();
    }

    @Test
    public void orderTest() throws Exception{
        // 下单查询订单记录
        mvc.perform(MockMvcRequestBuilders.get("/api/v1/batchPlaceOrder") // 测试的相对地址
                .contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk()) // 期待返回状态吗码200
                .andDo(print()) // 打印返回的 http response 信息
                .andReturn().getResponse().getContentAsString();
    }
}
